In [2]:
import pandas as pd
import datashader as ds
from util import gen_sparse_mat
import numpy as np
from scipy import sparse
from graph_reader import edgelist_to_csr
from bokeh.plotting import figure, output_notebook, show
import cPickle as pickle
from scipy.sparse import coo_matrix, csr_matrix
In [3]:
def permute(adjacency_matrix, labels):
    n = adjacency_matrix.shape[0]

    adjacency_matrix = coo_matrix(adjacency_matrix)
    row = np.copy(adjacency_matrix.row)
    col = np.copy(adjacency_matrix.col)
    val = np.copy(adjacency_matrix.data)
    nnz = len(val)

    matrix = csr_matrix(coo_matrix((val, (row, col)), shape = (n, n)))
    mapping = []
    for label in set(labels):
        mapping.extend(list(np.where(labels==label)[0]))

    return csr_matrix(coo_matrix( (val, ([mapping[r] for r in row], [mapping[c] for c in col])), (n, n)))
In [19]:
output_notebook()

def base_plot(dim):
    p = figure(
        x_range=(0, dim),
        y_range=(0, dim),
        tools='pan,wheel_zoom,box_zoom,reset', 
        plot_width=900, 
        plot_height=700,
    )
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None
    p.xaxis.axis_label = "col no."
    p.yaxis.axis_label = "row no."
    p.xaxis.axis_label_text_font_size = '12pt'
    p.yaxis.axis_label_text_font_size = '12pt'
    return p
Loading BokehJS ...
In [5]:
### Generate some data
adjacency_matrix = edgelist_to_csr("/Users/disanmhembere/Desktop/Data/hsb_results/1k/bio1k.edge")
Processed 0 edges ...

Adjacency matrix

In [6]:
adj_mat_rows, adj_mat_cols = adjacency_matrix.nonzero()
p = base_plot(adjacency_matrix.shape[0])
p.square(adj_mat_rows, adj_mat_cols, size=1)
show(p)
Out[6]:

<Bokeh Notebook handle for In[6]>

Shuffled matrix

In [7]:
shuffled_labels = np.array(range(adjacency_matrix.shape[0]))
np.random.shuffle(shuffled_labels)
shuffled_mat = permute(adjacency_matrix, shuffled_labels)
shuffled_rows, shuffled_cols = shuffled_mat.nonzero()
In [8]:
p = base_plot(shuffled_mat.shape[0])
p.square(shuffled_rows, shuffled_cols, size=1)
show(p)
Out[8]:

<Bokeh Notebook handle for In[8]>

Clustered Matrix

In [9]:
labels = pickle.load(open("/Users/disanmhembere/Desktop/Data/hsb_results/1k/rand_K+I_SBM_S40_k10_bio1k.p","rb")).labels_
permuted_matrix = permute(adjacency_matrix, labels)
p = base_plot(permuted_matrix.shape[0])
p.square(*permuted_matrix.nonzero(), size=1)
show(p)
Out[9]:

<Bokeh Notebook handle for In[9]>

In [10]:
pamk_labels = pickle.load(open("/Users/disanmhembere/Desktop/Data/hsb_results/1k_pamk/rand_PAMK_SBM_S40_k10_bio1k.p","rb"))
pamk_permuted_matrix = permute(adjacency_matrix, pamk_labels)
p = base_plot(pamk_permuted_matrix.shape[0])
p.square(*(pamk_permuted_matrix.nonzero()), size=1)
show(p)
Out[10]:

<Bokeh Notebook handle for In[10]>

In [12]:
hsbm_matrix = adjacency_matrix = edgelist_to_csr("/Users/disanmhembere/Desktop/Data/hsb_results/hsbm/hsbm.edge")
hsbm_matrix = hsbm_matrix + hsbm_matrix.T - sparse.diags(hsbm_matrix.diagonal())

HSBM matrix

In [17]:
p = base_plot(hsbm_matrix.shape[0])
p.square(*(hsbm_matrix.nonzero()), size=1)
show(p)
Out[17]:

<Bokeh Notebook handle for In[17]>

In [ ]:
hsbm_labels = pickle.load(open("/Users/disanmhembere/Desktop/Data/hsb_results/hsbm/rand_PAMK_SBM_S50_k7_hsbm.p", "rb"))
hsbm_permuted_matrix = permute(hsbm_matrix, hsbm_labels)
In [20]:
p = base_plot(hsbm_permuted_matrix.shape[0])
p.square(*(hsbm_permuted_matrix.nonzero()), size=1)
show(p)
Out[20]:

<Bokeh Notebook handle for In[20]>

In [ ]: